--  
-- Created by IntelliJ IDEA.  
-- User: lihua  
-- Date: 2016/2/29  
-- Time: 17:44  
-- To change this template use File | Settings | File Templates.  
-- 需要安装luasocket 才会支持 65上的安装地址是 /app/src/lj2/share/lua/5.1  
local http = require("socket.http")  
local ltn12 = require("ltn12")  
  
function HttpUtil()  
    local self = {}  
    -- get请求  
    self.httpget = function(u)  
        local t = {}  
        local r, c, h = http.request{  
            url = u,  
            -- 20160708 如果传入的是table的话， 就需要用一个容器来接收http body的内容， 也就是sink那个参数  
            sink = ltn12.sink.table(t)}  
        return r, c, h, table.concat(t)  
    end  
  
    self.httpPost = function(u,inParam)  
        -- 参考 http://www.stutostu.com/?p=1285  
        ngx.log(ngx.WARN,"url:"..u)  
        ngx.log(ngx.WARN,"inParam:"..inParam)  
        local response_body = {}  
        local post_data = inParam  
        local res, code = http.request{  
            url = u,  
            method = "POST",  
            headers =  
            {  
                ["Content-Type"] = "application/json",  
                ["Content-Length"] = #post_data,  
            },  
            source = ltn12.source.string(post_data),  
            sink = ltn12.sink.table(response_body)  
        }  
        res = table.concat(response_body)  
        ngx.log(ngx.WARN,"res:"..res)  
        ngx.log(ngx.WARN,"code:"..code)  
  
        return res,code  
  
    end  
  
    return self  
end 